package com.liy.cloud.base.rest;

import com.liy.cloud.base.entity.BaseEquParamEntity;
import com.liy.cloud.base.service.BaseEquParamService;
import com.das.framework.common.annotation.ActionPermission;
import com.das.framework.common.annotation.MethodPermission;
import com.das.framework.common.extvo.ResultVO;
import com.das.framework.common.vo.PageVO;
import com.das.framework.common.web.controller.BaseCrudController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @Author: zhangw
 * @Date: 2018/6/22  9:08
 * @Description: 设备参数管理
 **/
@RestController
@RequestMapping("/equparam")
@ActionPermission(code = "BASE.EQUPARAM", description = "设备参数管理")
public class BaseEquParamRest extends BaseCrudController<BaseEquParamService, BaseEquParamEntity>{

    protected static Logger log = LoggerFactory.getLogger(BaseEquParamRest.class);

    @Value("${server.port}")
    private String port;
    @Autowired
    private BaseEquParamService baseEquParamService;


    /**
     * @Description: 批量删除
     **/
    @PostMapping("/delete/batch")

    public ResultVO deleteBatch(@RequestBody() List<String> ids) {
        try {
            baseEquParamService.deleteBatch(ids);
            return new ResultVO(true, "删除成功:", "200");
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            return new ResultVO(false, "删除时发生错误:" + var4.getMessage(), "500");
        }
    }

    /**
     * 查询设备参数按设备分组
     *
     * @return
     * @auther zhangw
     * @date 2018/11/02 09:56
     */
    @ResponseBody
    @RequestMapping(value = {"get/equid/group"}, method = {RequestMethod.GET, RequestMethod.POST})
    public ResultVO getVersion() {
        ResultVO resultVO = new ResultVO();
        try{
            List<String> groupnameList = baseEquParamService.selectEquIdGroup();
            resultVO.setData(groupnameList);
        }catch (Exception e){
            log.error("生成参数版本时出错：", e);
            resultVO.setSuccess(false);
            resultVO.setMessage("生成参数版本时出错：" + e.getMessage());
        }
        return resultVO;
    }

    /**
     * 保存设备与种类参数关联
     *
     * @return
     * @auther zhangw
     * @date 2018/11/05 09:56
     */
    @RequestMapping(value = {"/save"}, method = {RequestMethod.POST})
    @ResponseBody
    @MethodPermission(codes = {"INSERT"}, description = "新增")
    public ResultVO save(@RequestBody Map<String, Object> params) {
        try {
            List<BaseEquParamEntity> typeParamList = (List<BaseEquParamEntity>) params.get("equParamList");
            String equId = (String) params.get("equId");
            baseEquParamService.save(typeParamList, equId);
            return new ResultVO(true, "保存成功", "200");
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            return new ResultVO(false, "新增时发生错误:" + var4.getMessage(), "500");
        }
    }


    /**
     * 点击树根据节点查询
     *
     * @param
     * @return
     * @auther zhangw
     * @date 2018/11/06 17:56
     */
    @RequestMapping(
            value = {"/query/treenode", "/query/treenode/{pageSize}", "/query/treenode/{pageSize}/{pageNo}"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    @MethodPermission(codes = {"SEARCH"}, description = "列表查询")
    public PageVO<BaseEquParamEntity> listByTreeNode(BaseEquParamEntity entity, @PathVariable(value = "pageSize",required = false) Integer pageSize, @PathVariable(value = "pageNo",required = false) Integer pageNo) {
        PageVO page = new PageVO(pageNo, pageSize);

        try {
            baseEquParamService.selectByTreeNodePage(entity, page);
        } catch (Exception var6) {
            log.error("查询数据出错!", var6);
            page.setStatus("500");
            page.setMessage(var6.getMessage());
        }

        return page;
    }

}
